package cn.alusiaor.eurekaclientoauth2server.controller;

import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/user")
@Slf4j
public class AuthenticationController {

    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request) {

        String name = SecurityContextHolder.getContext().getAuthentication().getName();



        String header = request.getHeader("Authorization");
        String token = header.substring("bearer ".length());
        return Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
    }


}
